# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : variable_view.py
# Time       ：2023/8/28 15:07
# Author     ：YangYong
# version    ：python 3.10
# Description：
    orderlines变量视图
"""
from flask_restx import fields

from apis.orderlines import orderlines_ns
from apis.orderlines.models import Variable
from apis.orderlines.schema.variable_schema import VariableSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_export_excel_view import BaseExportExcelView
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

variable = {
    'id': fields.String(description='变量ID'),
    'process_id': fields.String(description='流程ID'),
    'process_name': fields.String(description='流程名称'),
    'variable_key': fields.String(description='变量键'),
    'variable_value': fields.String(description='变量值'),
    'variable_desc': fields.String(description='变量描述信息'),
    'variable_type': fields.String(description='变量类型'),
    'is_cache': fields.Boolean(description='是否缓存', default=False),
}

variable_marshal = MarshalModel('od_variable', orderlines_ns)


class VariableDetailView(BaseDetailsView):
    url = '/variable/detail'

    def __init__(self, *args, **kwargs):
        super(VariableDetailView, self).__init__(*args, **kwargs)
        self.table_orm = Variable
        self.table_schema = VariableSchema

    @handle_api_error()
    @orderlines_ns.doc(description="获取变量详情")
    @orderlines_ns.expect(variable_marshal.detail_request())
    @orderlines_ns.marshal_with(variable_marshal.detail_response(variable))
    def get(self):
        """获取变量详情"""
        return super(VariableDetailView, self).get()


class VariableView(BaseView):
    url = '/variable'

    def __init__(self, *args, **kwargs):
        super(VariableView, self).__init__(*args, **kwargs)
        self.table_orm = Variable
        self.table_schema = VariableSchema
        self.is_logger = True

    def handle_filter(self):
        for key, val in self.form_data.items():
            if hasattr(self.table_orm, key) and val:
                self.filter.append(getattr(self.table_orm, key) == val)

    @handle_api_error()
    @orderlines_ns.doc(description='获取变量列表')
    @orderlines_ns.expect(variable_marshal.list_request(variable))
    @orderlines_ns.marshal_with(variable_marshal.list_response(variable))
    def get(self):
        """获取变量列表"""
        return super(VariableView, self).get()

    @handle_api_error()
    @orderlines_ns.doc(description='创建变量')
    @orderlines_ns.expect(variable_marshal.create_request(variable))
    @orderlines_ns.marshal_with(variable_marshal.create_response())
    def post(self):
        """创建变量"""
        return super(VariableView, self).post()

    @handle_api_error()
    @orderlines_ns.doc(description='修改变量')
    @orderlines_ns.expect(variable_marshal.update_request(variable))
    @orderlines_ns.marshal_with(variable_marshal.update_response())
    def put(self):
        """修改变量"""
        return super(VariableView, self).put()

    @handle_api_error()
    @orderlines_ns.doc(description='删除变量')
    @orderlines_ns.expect(variable_marshal.delete_request())
    @orderlines_ns.marshal_with(variable_marshal.delete_response())
    def delete(self):
        """删除变量"""
        return super(VariableView, self).delete()


class VariableReportView(BaseExportExcelView):
    url = '/variable/export'

    def __init__(self, *args, **kwargs):
        super(VariableReportView, self).__init__(*args, **kwargs)
        from datetime import datetime
        self.file_name = f'task_{datetime.now().strftime("%Y-%m-%d")}'
        self.columns = {
            'id': '序号',
            'process_id': '流程id',
            'process_name': '流程名称',
            'variable_key': '变量名称',
            'variable_value': '变量值',
            'variable_desc': '变量描述',
            'variable_type': '变量类型',
            'insert_time': '创建时间',
            'update_time': '修改时间',
            'creator': '创建者',
        }
        self.is_logger = True
        self.post_action = '导出变量报告'

    @handle_api_error()
    @orderlines_ns.doc(description='导出变量信息')
    @orderlines_ns.expect(variable_marshal.list_request(variable))
    def post(self):
        """导出变量信息"""
        return super(VariableReportView, self).post()
